<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\Area;
use app\admin\model\Server;
use app\admin\validate\CheckCompany;
use think\exception\ValidateException;
use think\facade\Db;
class Company extends Base
{

    protected $mod;

    protected function initialize()
    {
        parent::initialize();
        $this->mod = new \app\admin\model\Company;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($query=[],int $page=1,int $limit=50)
    {
        $map=[];
        $company = $this->mod;
        if($query){

            foreach($query as $k=>$v){
                if($v['col'] == 'area'){
                    if(!empty(json_decode($v['value']))){
                        $area = array_map('intval', json_decode($v['value']));
                        $where = Area::where('area_id',end($area));
                        $company = $this->mod::hasWhere('companyArea',$where);
                    }
                }else{
                    $value=$v['value'];
                    $map[]=[$v['col'],'like',"%$value%"];
                }

            }
        }
        $roles_data = [];
        foreach ($this->admin->roles as $role) {
            $roles_data[] = $role->id;
        }
        if(in_array(5,$roles_data)){
            $map[] = ['admin_id','=',$this->request->uid];
        }
        $dataList = $company->where($map)->append(['admin_id_name'])->order('update_time desc')->paginate($limit, false, ['page' => $page, 'query' => $query]);
        return $this->success('ok', $dataList);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data = $this->request->param();
        $data['admin_id']=$this->admin->id;
        try {
            //验证
            $valCheck = validate(CheckCompany::class)->scene('add')->check($data);
            if ($valCheck !== true) {
               return $this->error($valCheck);
            }
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        $company = $this->mod::create($data);
        if ($company) {
            return $this->success('添加成功');
        } else {
            return $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $res=$this->mod::with(['companyArea','companyServer'])->where('id',$id)->append(['thumbFile','areaName'])->find();
        return $this->success('ok',$res);
    }


    /**
     * 更新企业信息
     */
    public function update( int $id=0)
    {
        if($this->request->isPost()){
            $data=$this->request->param();
            if($res=$this->mod::update($data)){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }

    }
    /**
     * 更新企业服务器信息
     */
    public function updateServer()
    {
        $data = $this->request->param();
        $server = new Server();
        $data['company_id'] = (int)$data['company_id'];
        //查询服务器状态
        $status = $this->mod::where('id',$data['company_id'])->value('status');
        if(empty($status)){
            return $this->error('请检查企业状态!');
        }
        if(isset($data['id'])){
            $data['id'] = (int)$data['id'];
            $res = $server->update($data);
        }else{
            $res = $server->save($data);
        }
        if($res){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }

    }



    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($ids = [])
    {
        if (!$ids) {
            return 0;
        }
        if (is_array($ids)) {
            $ids_data = array_map('intval', $ids);
        }else{
            $ids_data = explode(',',$ids);
        }
        foreach ($ids_data as $value){
            $company = $this->mod::with(['companyArea','server'])->find($value);
            $company->together(['companyArea','server'])->delete();
        }
        return $this->success("删除完毕");
    }
    public function setStatus()
    {
        if($this->request->isPost()){
            $data=$this->request->param();
            foreach ($data as &$v){
               $v = (int)$v;
            }
            if($res=$this->mod::update($data)){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }

    }

    /**查看编辑获取企业信息
     * @return void
     */
    public function getData($id)
    {
        $res=$this->mod::with(['companyArea'])->where('id',$id)->append(['thumbFile','areaName'])->hidden(['status','endtime'])->find();
        return $this->success('ok',$res);
    }
}
